
var page=1;
var pagePage=""
// 日历

var startTime="",
    endTime="",
    duringDay, startDate, endDate,checkTime;
laydate.render({
    elem: '#enter_time', //指定元素
    format: 'yyyy年M月dd日',
    range: '~',
    min: 0, //今天起
    done: function(value, formDate,endDate){
        var str = formDate.year + '/' + formDate.month + '/' + formDate.date;
        var strE= endDate.year+ '/' + endDate.month + '/' + endDate.date;
        var datex = new Date(str);
        var datey = new Date(strE);
        var $totalTime = $('.totalTime');
        
        startTime = parseInt(datex.getTime());
        // console.log(startTime);
        endTime = parseInt(datey.getTime());
        // console.log(endTime);
        var duringTime = endTime - startTime;
        checkTime=duringTime;
        console.log(checkTime);
        if (duringTime <= 0) {
            $totalTime.html('请选择入住日期以后的日期！')
        } else {
            duringDay = parseInt(duringTime / 86400000)
            $totalTime.html('共' + duringDay + '晚');
        }
    }
});

var status="";
$(".payList_title li").click(function(){
    $(this).css({"background":"#5944C3","color":"#fff"}).siblings().css({"background":"#F1F1F1","color":"#3E3E3E"});
    status=$(this).attr("data-type");
    $(".Myorderform").find("ul").remove();
    ajax(page);
    console.log(status);
})
// 封装ajax请求
function ajax(page){
    var orderNo=$(".orderNo").val();
    var customerName=$(".customerName").val();
    $.ajax({
        url:url+"v1/order/list",
        dataType:"json",
        method:"GET",
        data:{
            token:localStorage.token,
            status:status,
            orderNo:orderNo,
            customerName:customerName,
            checkTime:checkTime,
            limit:4,
            page:page
        },
        xhrFields: {
            withCredentials: true // 允许跨域名储存和访问cookie
        }
    }).done(function(data){
        $(".Myorderform ul").remove();
        console.log(data); 
        var page_count=Math.ceil(data.data.count/4);
        console.log(page_count);
        if(data.code=="success"){
            var payList=data.data.orderList;
            var str="";
            for(i=0;i<payList.length;i++){
                // 转换下单时间戳
                var btns="";
                var create_time=payList[i].create_time;
                create_time=moment(create_time).format('YYYY-MM-DD');

                var payId=payList[i]._id;
                // console.log("下单时间"+create_time);
                if(payList[i].status=="预定中"){
                    btns+=`<a href="paydetail.html?payId=`+payId+`">查看</a><a href="payCancel.html?payId=`+payId+`" class="no_reservation">取消预订</a><a class="done" data-id="`+payId+`">完成</a>`;
                }else if(payList[i].status=="待支付"){
                    btns+=`<a href="paydetail.html?payId=`+payId+`">查看</a><a href="pay.html?payId=`+payId+`" class="toPay">去支付</a>`;
                }else if(payList[i].status=="已完成"){
                    btns+=`<a href="paydetail.html?payId=`+payId+`">查看</a><a href="payEvaluate.html?payId=`+payId+`?hotelPic=`+payList[i].hotel.picture[0]+`?hotelName=`+payList[i].hotel.name+`?roomName=`+payList[i].roomName+`?amount=`+payList[i].amount+`" class="to_evaluate">评价</a>`;
                }else if(payList[i].status=="已取消"){
                    btns+=`<a href="paydetail.html?payId=`+payId+`">查看</a>`;
                }
                str+=`<ul class="clearfix">
                        <li>`+payList[i].orderNo +`</li>
                        <li class="names">
                            <div class="hotelName" title="`+payList[i].hotel.name+`">`+payList[i].hotel.name+`</div>
                            <div class="roomName">`+payList[i].roomName+`</div>
                        </li>
                        <li>`+create_time+`</li>
                        <li>`+payList[i].customerName+`</li>
                        <li>￥`+payList[i].amount+`</li>
                        <li>`+payList[i].status+`</li>
                        <li class="btns">`+btns+`</li>
                      </ul>`;                      
                
            }
            $(".Myorderform").append(str);


            // 分页器！
            // $(".pagination").on("click","li",function(){
            //     $(this).addClass("checked").siblings().removeClass("checked");
            // })
            // $(".count_page").html(page_count);
            $('.box').pagination({
                pageCount: page_count,
                jump: true,
                coping: true,
                homePage: '首页',
                endPage: '末页',
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    console.log(api.getCurrent())
                    
                    page = api.getCurrent();
                    ajax1(page);
                }
            });
        }else{
            console.log(data.message);
        }
    })
}
function ajax1(page){
    var orderNo=$(".orderNo").val();
    var customerName=$(".customerName").val();
    $.ajax({
        url:url+"v1/order/list",
        dataType:"json",
        method:"GET",
        data:{
            token:localStorage.token,
            status:status,
            orderNo:orderNo,
            customerName:customerName,
            checkTime:checkTime,
            limit:4,
            page:page
        },
        xhrFields: {
            withCredentials: true // 允许跨域名储存和访问cookie
        }
    }).done(function(data){
        $(".Myorderform ul").remove();
        console.log(data); 
        var page_count=Math.ceil(data.data.count/4);
        console.log(page_count);
        if(data.code=="success"){
            var payList=data.data.orderList;
            var str = "";
            for(i=0;i<payList.length;i++){
                // 转换下单时间戳
                var btns="";
                var create_time=payList[i].create_time;
                create_time=moment(create_time).format('YYYY-MM-DD');

                var payId=payList[i]._id;
                // console.log("下单时间"+create_time);
                if(payList[i].status=="预定中"){
                    btns+=`<a href="paydetail.html?payId=`+payId+`">查看</a><a href="payCancel.html?payId=`+payId+`" class="no_reservation">取消预订</a><a class="done" data-id="`+payId+`">完成</a>`;
                }else if(payList[i].status=="待支付"){
                    btns+=`<a href="paydetail.html?payId=`+payId+`">查看</a><a href="pay.html?payId=`+payId+`" class="toPay">去支付</a>`;
                }else if(payList[i].status=="已完成"){
                    btns+=`<a href="paydetail.html?payId=`+payId+`">查看</a><a href="payEvaluate.html?payId=`+payId+`?hotelPic=`+payList[i].hotel.picture[0]+`?hotelName=`+payList[i].hotel.name+`?roomName=`+payList[i].roomName+`?amount=`+payList[i].amount+`" class="to_evaluate">评价</a>`;
                }else if(payList[i].status=="已取消"){
                    btns+=`<a href="paydetail.html?payId=`+payId+`">查看</a>`;
                }   
                str+=`<ul class="clearfix">
                <li>`+payList[i].orderNo +`</li>
                <li class="names">
                    <div class="hotelName" title="`+payList[i].hotel.name+`">`+payList[i].hotel.name+`</div>
                    <div class="roomName">`+payList[i].roomName+`</div>
                </li>
                <li>`+create_time+`</li>
                <li>`+payList[i].customerName+`</li>
                <li>￥`+payList[i].amount+`</li>
                <li>`+payList[i].status+`</li>
                <li class="btns">`+btns+`</li>
              </ul>`;               
            }
            $(".Myorderform").append(str);
        }else{
            console.log(data.message);
        }
    })
}
console.log(pagePage);
$(".Myorderform").on("click",".done",function(){
    console.log($(this).attr("data-id"))
    $.ajax({
        url:url+"v1/order/complete",
        dataType:"json",
        method:"POST",
        data:{
            orderId:$(this).attr("data-id")
        },
        xhrFields: {
            withCredentials: true // 允许跨域名储存和访问cookie
        }
    }).done(function(data){
        if(data.code=="success"){
            alert("订单已完成！")
            window.location.href="payList.html";
        }
    })
})

$(document).ready(function(){
    var page_count="";
    ajax(page);
    pagePage=page_count;
})